package com.xys.auth.mapper;

import com.xys.auth.bean.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleMapper {

    @Select("select * from role where id in (select role_id from users_role where user_id=#{userId})")
    @Results({

            @Result(id = true,column = "id",property = "id"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc"),


            @Result(column = "id",property = "permissions",
                   many = @Many(select = "com.xys.auth.mapper.PermissionMapper.findByRoleId")
            )
    })
    List<Role> findByUserId(@Param("userId") String userId);

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role values (#{id},#{roleName},#{roleDesc})")
    int insert(Role role);


    @Select("select * from role where id not in (select role_id from users_role where user_id=#{userId})")
    List<Role> findOtherRole(@Param("userId") String id);

    @Insert("insert into role_permission values(#{permissionId},#{roleId})")
    int addPermissionToRole(@Param("roleId") String roleId,@Param("permissionId")String id);

    @Select("select * from role where id=#{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc"),
            @Result(column = "id" ,property = "permissions",
                    many = @Many(select = "com.xys.auth.mapper.PermissionMapper.findByRoleId"))
    })
    Role findById(String id);
}
